package com.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class TestSort {

	public static void main(String[] args) {
		List<SortInf> sortList = new ArrayList<SortInf>();
		SortInf st1 = new SortObj1("ddd");
		SortInf st2 = new SortObj2("ccc");
		SortInf st3 = new SortObj2("aaa");
		SortInf st4 = new SortObj1("bbb");
		sortList.add(st1);
		sortList.add(st2);
		sortList.add(st3);
		sortList.add(st4);
		for(SortInf lst : sortList){
			lst.showObj();
		}
		System.out.println("after sort!-------------------------");
		Comparator myCompare = new MyComparetor();
		Collections.sort(sortList, myCompare);
		for(SortInf lst : sortList){
			lst.showObj();
		}
	}
	
	
}

class MyComparetor implements Comparator<Object>{
	@Override
	public int compare(Object o1, Object o2) {
		SortInf st1 = (SortInf) o1;
		SortInf st2 = (SortInf) o2;
		int index = st1.getUserName().compareTo(st2.getUserName());
		if(index>0){
			return 1;
		}else if(index<0){
			return -1;
		}else{
			return 0;
		}
	}
	
}